package com.zjx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zjx.reggie.common.R;
import com.zjx.reggie.entity.ShoppingCart;
import com.zjx.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService service;

    @GetMapping("/list")
    public R list(HttpServletRequest request,ShoppingCart shoppingCart){
        String userId = request.getSession().getAttribute("userId").toString();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        qw.groupBy(ShoppingCart::getName);
        List<ShoppingCart> list = service.list(qw);
        return R.success(list);
    }

    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        String userId = request.getSession().getAttribute("userId").toString();
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        if(userId != null){
            qw.eq(ShoppingCart::getUserId,userId);
            qw.eq(ShoppingCart::getName,shoppingCart.getName());
        }
        ShoppingCart sc = service.getOne(qw);
        Integer num = 0;
        if(sc != null){
            num = sc.getNumber()+1;
            LambdaUpdateWrapper<ShoppingCart> sh = new LambdaUpdateWrapper<>();
            sh.eq(ShoppingCart::getId,sc.getId());
            sh.set(ShoppingCart::getNumber,num);
            shoppingCart.setNumber(num);
            service.update(sh);
        }else if(sc == null){
            service.save(shoppingCart);
            shoppingCart.setNumber(1);
        }else{
            R.error("购物车添加失败！");
        }
        sc = shoppingCart;
        return R.success(sc);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request){
        String userId = (String)request.getSession().getAttribute("userId");
        LambdaUpdateWrapper<ShoppingCart> qw = new LambdaUpdateWrapper<>();
        qw.eq(StringUtils.isNotEmpty(shoppingCart.getDishId()),ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(StringUtils.isNotEmpty(shoppingCart.getSetmealId()),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId,userId);

        ShoppingCart one = service.getOne(qw);
        Integer num = 0;
        if(one != null && one.getNumber() > 1 ){
            num = one.getNumber()-1;
            qw.set(ShoppingCart::getNumber,num);
            shoppingCart.setNumber(num);
            service.update(qw);
        }else if(one != null && one.getNumber() == 1){
            shoppingCart.setNumber(null);
            service.removeById(one.getId());
            R.error("没有本次购物车数据！");
        }
        one = shoppingCart;
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R clean(HttpServletRequest request){
        String userId = request.getSession().getAttribute("userId").toString();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        service.remove(qw);
        return R.success("购物车清空成功！");
    }
}
